<?php
/**
 * Created by PhpStorm.
 * User: AivyYuan
 * Date: 2016/6/30
 * Time: 16:58
 */
namespace app\controllers;

use app\models\Blog;
use app\models\BlogComment;
use app\models\Category;
use app\models\CategoryForm;
use Yii;
use yii\base\UserException;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use yii\bootstrap\Alert;
use yii\data\Pagination;
class BlogController extends Controller{
    //blog 首页
    public $layout = 'blog';
    public function behaviors()
    {
       return [
           'access'=>[
               'class'=>AccessControl::className(),
               'only' => ['add','Categoryadd'],
               'rules'=>[
                   [
                       'allow'=>true,
                       'actions'=>['add','Categoryadd'],
                       'roles'=>['@'],
                   ]
               ]
           ]
       ];
    }

    public function actionIndex(){
        $blog_model = new Blog();
        $query = $blog_model->find()->joinWith('content')->joinWith('user');
        $count = $query->count();
        $pagination = new Pagination(['totalCount' => $count]);
        $pagination->setPageSize(10);
        $list = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        



        return $this->render('index',['list'=>$list,'pagination'=>$pagination]);
    }
    //添加blog
    public function actionAdd(){
        if(Yii::$app->request->isPost){
            $blog_model = new Blog();
            $data = Yii::$app->request->post();
            $data['addtime'] = time();
            $data['user_id'] = \Yii::$app->user->getId();
           $result = $blog_model->saveBlog($data) ;
            
            if($result){
               return $this->refresh();
            }else{
                $data = ['message'=>$blog_model->getErrors(),'url'=>'blog/add'];
                return $this->render("error",['data'=>$data]);
            }

        }
        $model = Category::findAll(['status'=>1,"pid"=>0]);
        
        return $this->render('add',['model'=>$model]);
    }
    public function actionCategoryadd(){
        
        if(Yii::$app->request->isGet){

            $model = Category::findAll(['status'=>1,"pid"=>0]);

            return $this->render("category_add",["model"=>$model]);
        }else{

            $model = new Category();
            $data[$model->formName()] = Yii::$app->request->post();

            $model->load($data);
            if($model->validate()){
                $model->name = $model->name;
                $model->status = $model->status;
                $model->desc = $model->desc;
                $model->pid = $model->pid;
                $model->save();
                return $this->refresh();
            }else{
                $data = ['message'=>$model->getErrors(),'url'=>'blog/categoryadd'];
                return $this->render("error",['data'=>$data]);
            }
        }

    }
    public function actionCategorysave(){
        echo 'abc';
    }

    public function actionChild($pid = 0){

        static $level = "-";
        $category = Category::findAll(['pid'=>$pid]);

        if(is_array($category) && $category){

            $level .='-';
            foreach ($category as $k=>$v){
                echo "<option value='$v->id'>".$level.$v->name."</option>";

                $this->actionChild($v->id);
            }

        }else{
            $level = '-';
        }


    }

    public function actionComment(){
        if(Yii::$app->request->isPost){
            $content = Yii::$app->request->post('content');
            $comment_model = new BlogComment();
            $comment_model->blog_id = Yii::$app->request->post('blog_id');
            $comment_model->user_id = Yii::$app->user->getId();
            $comment_model->content = $content;
            $comment_model->addtime = time();
            $comment_model->status = 1;
            $comment_model->save();
            $this->refresh();

            
        }else{
            $id = Yii::$app->request->get('id');
            if(!$id){
                throw new \yii\web\ NotFoundHttpException("请求出错");
            }
            $comment_model = new BlogComment();
            $comment_list = $comment_model->findAll(array("blog_id"=>$id));

            return $this->render("comment",["id"=>$id,"comment_list"=>$comment_list]);
        }


    }



}